import sys
import RPi.GPIO as GPIO

from bases import globalvar
from bases.config import *

'''
GPIO 控制
'''


class IoCtrl:
    '''
    初始化IO口
    '''

    @staticmethod
    def gpioinit():
        GPIO.setwarnings(False)
        GPIO.setmode(GPIO.BCM)
        GPIO.setup(Din1, GPIO.IN)
        GPIO.setup(Din2, GPIO.IN, pull_up_down=GPIO.PUD_UP)  # GPIO[26]行程开关控制口
        GPIO.setup(LedPin, GPIO.OUT)
        GPIO.output(LedPin, 0)

    '''
    补光灯延迟关闭
    '''

    @staticmethod
    def delay():
        ledLightTime = globalvar.get_value('ledLightTime')
        # print(ledLightTime)
        if (ledLightTime is not None):
            if (ledLightTime > 0):
                GPIO.output(12, 1)
                ledLightTime = ledLightTime - 100
                globalvar.set_value('ledLightTime', ledLightTime)
            else:
                GPIO.output(12, 0)

    '''
        开启补光灯，延迟自动关闭
    '''

    @staticmethod
    def fillLightWithDelay():
        GPIO.output(12, 1)
        # 全局变量， 补光灯5000ms后关闭
        globalvar.set_value('ledLightTime', 5000)

    '''
        开启补光灯，等待手动关闭
    '''

    @staticmethod
    def lightOpen():
        GPIO.output(12, 1)
        globalvar.set_value('ledLightTime', None)

    '''
        开启补光灯，等待手动关闭
    '''

    @staticmethod
    def lightClose():
        GPIO.output(12, 0)